<?php

namespace App\Models\Collection;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * App\Models\RepayRecordMoney
 *
 * @property int $id
 * @property int $uid 关联用户id
 * @property int $summary_id 关联还款表id
 * @property int $record_id 关联还款记录表id
 * @property int $bank_cards_id 关联user_bank_cards银行卡id
 * @property string $third_repay_id 资方接口返回,还款请求id
 * @property string $trade_no 平台内唯一流水号
 * @property int $period 当前期数,0为全部还清记录
 * @property int $lending_money 利息费,以分为单位
 * @property int $fines_money 罚息费,以分为单位
 * @property int $manage_money 贷后管理费,以分为单位
 * @property int $repayment_money 提前还款手续费,以分为单位
 * @property int $failure 失败原因, 枚举
 * @property int $status 蔚捷接口状态 1:待处理,2:处理中,3:失败,4:成功
 * @property int $type 成功详细状态 1:,2:,3:,4:
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereBankCardsId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereFailure($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereFinesMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereLendingMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereManageMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney wherePeriod($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereRecordId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereRepaymentMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereSummaryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereThirdRepayId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereTradeNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $apply_time 接口申请时间
 * @property string|null $status_time 接口处理成功/失败时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereApplyTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereStatusTime($value)
 * @property-read \App\Models\RepayRecordCommon $common
 * @property-read \App\Models\RepayRecordMonth $month
 * @property-read \App\Models\RepayRecord $record
 * @property-read \App\Models\UserCertOcr $relationUserCertOcrs
 * @property-read \App\Models\User $relationUsers
 * @property int $repay_type 1=>app中用户手动点击还款,2=>app中自动还款
 * @property string|null $repay_date 资方真实还款日期
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereRepayDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\RepayRecordMoney whereRepayType($value)
 */
class RepayRecordMoney extends Model
{

    //蔚捷资金接口状态 1:待处理,2:处理中,3:失败,4:成功
    const STATUS_ONE = 1;
    const STATUS_TWO = 2;
    const STATUS_THREE = 3;
    const STATUS_FOUR = 4;

    //接口成功类型 map 还款计划表成功类型
    const TYPE_ONE = 1; //NN 待还
    const TYPE_TWO = 2; //NR 正常还款
    const TYPE_THREE = 3; //NT 当期提前还款
    const TYPE_FOUR = 4; //NJ 提前结清
    const TYPE_FIVE = 5; //ON 逾期
    const TYPE_SIX = 6; //OR 逾期还款
    const TYPE_SEVEN = 7; //DN 代偿
    const TYPE_EIGHT = 8; //DR 代偿还款
    const TYPE_NINE = 9; //DD 代偿期代偿
    const TYPE_TEN = 10; //ND 正常期代偿
    const TYPE_ELEVEN = 11; //OD 逾期代偿


    /**
     * 枚举成功类型数据
     * */
    static $TYPE_ENUM = array(
        self::TYPE_ONE        => '待还',
        self::TYPE_FIVE => '逾期',
        self::TYPE_TWO       => '逐期还清',
        self::TYPE_FOUR          => '提前结清',
    );


    /**
     * 催收链接
     */
    protected $connection='backend';

    /**
     * 第三方状态对照表
     *
     * @var array
     */
    public static $third_map_type = [
        'NN' => self::TYPE_ONE,
        'NR' => self::TYPE_TWO,
        'NT' => self::TYPE_THREE,
        'NJ' => self::TYPE_FOUR,
        'ON' => self::TYPE_FIVE,
        'OR' => self::TYPE_SIX,
        'DN' => self::TYPE_SEVEN,
        'DR' => self::TYPE_EIGHT,
        'DD' => self::TYPE_NINE,
        'ND' => self::TYPE_TEN,
        'OD' => self::TYPE_ELEVEN,
    ];

    /**
     * 定义不允许自动填充的字段
     * @var array
     */
    protected $guarded = [
    ];

    /**
     * Description: 设置资方记录利息费
     * Author: Gxs
     * @param $value
     */
    public function setLendingMoneyAttribute($value)
    {
        $this->attributes['lending_money'] = (int)bcmul($value, 100);
    }

    /**
     * Description: 获取资方记录利息费
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getLendingMoneyAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    /**
     * Description: 设置资方记录罚息费
     * Author: Gxs
     * @param $value
     */
    public function setFinesMoneyAttribute($value)
    {
        $this->attributes['fines_money'] = (int)bcmul($value, 100);
    }

    /**
     * Description: 获取资方记录罚息费
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getFinesMoneyAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    /**
     * Description: 设置资方记录贷后管理费
     * Author: Gxs
     * @param $value
     */
    public function setManageMoneyAttribute($value)
    {
        $this->attributes['manage_money'] = (int)bcmul($value, 100);
    }

    /**
     * Description: 获取资方记录贷后管理费
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getManageMoneyAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    /**
     * Description: 设置资方记录提前还款手续费
     * Author: Gxs
     * @param $value
     */
    public function setRepaymentMoneyAttribute($value)
    {
        $this->attributes['repayment_money'] = (int)bcmul($value, 100);
    }

    /**
     * Description: 获取资方记录提前还款手续费
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getRepaymentMoneyAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    /**
     * Description: 关联记录接口表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function record()
    {
        return $this->hasOne('App\Models\Collection\RepayRecord', 'id', 'record_id');
    }

    /**
     * Description: 关联月报接口表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function month()
    {
        return $this->hasOne('App\Models\Collection\RepayRecordMonth', 'record_id', 'record_id');
    }

    /**
     * Description: 关联公共接口表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function common()
    {
        return $this->hasOne('App\Models\Collection\RepayRecordCommon', 'record_id', 'record_id');
    }

    /**
     * Description: 反关summary
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationSummary()
    {
        return $this->belongsTo('App\Models\Collection\RepaySummary', 'summary_id', 'id');
    }

    /**
     * Description: 反关用户表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationUsers()
    {
        return $this->belongsTo('App\Models\User', 'uid', 'id');
    }

    /**
     * Description: 反关用户身份证实名表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function relationUserCertOcrs()
    {
        return $this->belongsTo('App\Models\UserCertOcr', 'uid', 'user_id');
    }

    public function getLoanIdAttribute(){
        $summary_id = $this->summary_id;
        if(empty($summary_id)){
            $summary_id = self::where('id',$this->id)->value('summary_id');
        }
        return \App\Models\RepaySummary::where('id',$summary_id)->value('loan_id');
    }
}